package com.lin.chapter36;


import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

public class EventDispatch implements Observer{
    private final static EventDispatch dispatch = new EventDispatch();
    private Vector<EventCustomer> customers = new Vector<>();

    private EventDispatch(){
    }

    public static EventDispatch getEventDispatch(){
        return dispatch;
    }


    @Override
    public void update(Observable o, Object arg) {
        Product product = (Product) arg;
        ProductEvent event = (ProductEvent) o;
        for (EventCustomer customer : customers) {
            for (EventCustomType eventCustomType : customer.getCustomTypes()) {
                if (eventCustomType.getValue() == event.getEventType().getValue()){
                    customer.exec(event);
                }
            }
        }
    }

    public void registerCustomer(EventCustomer customer){
        customers.add(customer);
    }
}
